package com.yc.hr.ui;

import java.awt.EventQueue;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;

import com.yc.hr.biz.EmpBiz;
import com.yc.hr.util.UIUtils;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.CardLayout;
import javax.swing.ImageIcon;
import javax.swing.JDialog;

public class MainWin {

	private JFrame frame;
	private DeptPanel deptPanel = new DeptPanel();
	private EmpPanel empPanel = new EmpPanel();
	private SalGradePanel salGradePanel = new SalGradePanel();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWin window = new MainWin();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWin() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 652, 486);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnNewMenu = new JMenu("基础数据管理");
		menuBar.add(mnNewMenu);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("部门管理");
		
		mnNewMenu.add(mntmNewMenuItem);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("工资等级");
		
		mnNewMenu.add(mntmNewMenuItem_1);
		
		JMenuItem mntmNewMenuItem_2 = new JMenuItem("查询员工");
		mnNewMenu.add(mntmNewMenuItem_2);
		
		JMenu mnNewMenu_1 = new JMenu("人事管理");
		menuBar.add(mnNewMenu_1);
		
		JMenu mnNewMenu_2 = new JMenu("考勤管理");
		mnNewMenu_1.add(mnNewMenu_2);
		
		JMenuItem mntmNewMenuItem_3 = new JMenuItem("请假");
		mnNewMenu_2.add(mntmNewMenuItem_3);
		
		JMenuItem mntmNewMenuItem_4 = new JMenuItem("放假");
		mnNewMenu_2.add(mntmNewMenuItem_4);
		
		JMenuItem mntmNewMenuItem_5 = new JMenuItem("员工入职");
		mnNewMenu_1.add(mntmNewMenuItem_5);
		
		JMenuItem mntmNewMenuItem_6 = new JMenuItem("员工离职");
		mnNewMenu_1.add(mntmNewMenuItem_6);
		
		JMenu mnNewMenu_3 = new JMenu("帮助");
		menuBar.add(mnNewMenu_3);
		
		JMenuItem mntmNewMenuItem_7 = new JMenuItem("关于我们");
		mnNewMenu_3.add(mntmNewMenuItem_7);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
		frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("首页", null, panel, null);
		panel.setLayout(new CardLayout(0, 0));
		
		JLabel lblNewLabel = new JLabel("") {
			private static final long serialVersionUID = 1L;
			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				// this == lblNewLabel 同一个对象  
				UIUtils.paintComponent(g, this, "D:\\548e248780a19d6.jpg");
			}
			
		};
//		lblNewLabel.setIcon(new ImageIcon("D:\\548e248780a19d6.jpg"));
		panel.add(lblNewLabel, "name_13659566151400");
		
		//////////////////////////////
		Map<String,JPanel> panels = new HashMap<>();
		panels.put("部门管理", deptPanel);
		panels.put("工资等级", salGradePanel);
		panels.put("查询员工", empPanel);
		// 还可以继续添加其他面板
		
		ActionListener actionListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 获取事件源对象 => 菜单
				JMenuItem item = (JMenuItem) e.getSource();
				// 获取菜单名
				String name = item.getText();
				
				// 判断当界面是否已经打开
				// 获取tab总数
				int tabCount = tabbedPane.getTabCount();
				for (int i = 0; i < tabCount; i++) {
					// 判断标题是否和当前菜单相同
					if(tabbedPane.getTitleAt(i).equals(name)) {
						// 该界面已经创建, 直接显示出来就ok
						tabbedPane.setSelectedIndex(i);
						return;
					}
				}
				// for 循环结束, 表示该界面没有打开, 则创建
//				JPanel panel = new JPanel();
//				DeptPanel panel = new DeptPanel();
				JPanel panel = panels.get(name);
				
				tabbedPane.addTab(name, null, panel, "这是: " + name);
				// 选择刚才创建的tab
				tabbedPane.setSelectedIndex(tabCount);
			}
		};
		
		mntmNewMenuItem.addActionListener(actionListener);
		mntmNewMenuItem_1.addActionListener(actionListener);
		mntmNewMenuItem_2.addActionListener(actionListener);
		
		// 设置窗口居中
		UIUtils.setCenter(frame);
		
		frame.setVisible(true);
		
		LoginDlg dialog = new LoginDlg();
		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		dialog.setVisible(true);
		
		frame.setTitle("欢迎" + EmpBiz.getLoginedUser().get("ENAME"));
		
	}

}
